/*
 * All content copyright http://www.j2eefast.com, unless
 * otherwise indicated. All rights reserved.
 * No deletion without permission
 */
package com.j2eefast.flowable.bpm.controller;

import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.common.core.utils.RedisUtil;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.flowable.bpm.service.BpmFormDraftService;
import com.j2eefast.flowable.bpm.service.FormdefService;
import com.j2eefast.framework.annotation.RepeatSubmit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.servlet.ShiroHttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>待发流程</p>
 *
 * @author: zhouzhou
 * @date: 2020-04-19 00:02
 * @web: http://www.j2eefast.com
 * @version: 1.0.1
 */
@Controller
@RequestMapping("/bpm/formdraft")
public class BpmFormDraftController extends BaseController {

	private String urlPrefix = "modules/bpm/formdraft";

	@Autowired
	private BpmFormDraftService bpmFormDraftService;


	@Autowired
	private RedisUtil redisUtil;

	@RequestMapping("/list")
	@RequiresPermissions("bpm:formdraft:list")
	@ResponseBody
	public ResponseData list(@RequestParam Map<String, Object> params, ShiroHttpServletRequest request) {
		//只查找出待发流程
		PageUtil page = bpmFormDraftService.findPage(params);
		String sessionId=request.getRequestedSessionId();
		String test=redisUtil.getSession("sys_csrfToken:shiro:session:"+sessionId).toString();
		ResponseData responseData=success(page);
		responseData.put("X-CSRF-Token",test);
		return responseData;
//		return success(page);
	}

	@RequiresPermissions("bpm:formdraft:list")
	@GetMapping()
	public String form() {
		return urlPrefix + "/formlist";
	}

	@RequiresPermissions("bpm:formdraft:list")
	@GetMapping("/preview/{id}")
	public String preview(@PathVariable("id") String id, ModelMap mmap) {
		mmap.put("id",id);
		return  urlPrefix + "/view";
	}


	/**
	 * 删除流程定义
	 */
	@RepeatSubmit
	@BussinessLog(title = "流程定义", businessType = BusinessType.DELETE)
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	@RequiresPermissions("bpm:formdraft:del")
	@ResponseBody
	public ResponseData del(HttpServletRequest request) {
		Long formDraftId=Long.parseLong(request.getParameter("formDraftId"));

		return bpmFormDraftService.deleteFormById(formDraftId)?success():error("删除失败!");
	}




}
